package com.yxq.mdt.app.modules.classification.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.hawk.Hawk;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.IntentHelper;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.RouteTable;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.classification.bean.ClassificationBean;
import com.yxq.mdt.app.modules.classification.model.ClassificationModel;
import com.yxq.mdt.app.modules.classification.prestener.ClassificationPresenter;
import com.yxq.mdt.app.modules.classification.prestener.IClassificationContract;

import java.util.List;

import butterknife.BindView;

/**
 * @author L
 * @time
 * @describe 全部分类
 */
@Route(path = RouteTable.ROUTER_ACTIVITY_CLASSIFICATION)
public class ClassificationActivity extends BaseActivity<ClassificationPresenter> implements IClassificationContract.View {

    @BindView(R.id.class_recycler_view)
    RecyclerView mMenuView;
    @BindView(R.id.class_child_recycler_view)
    RecyclerView mChildView;
    @BindView(R.id.class_iv_banner)
    ImageView mImageBanner;
    ClassificationMenuListAdapter mClassificationMenuListAdapter;
    ClassificationChildMenuListAdapter mClassificationChildMenuListAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_classification;
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        List<String> keys = getHisSearchTags();
        mCommonTitleBar.getCenterSearchEditText().setHint((keys != null && keys.size() > 0) ? keys.get(0) : "搜索一下");
        mCommonTitleBar.getCenterSearchEditText().setOnClickListener(view -> IntentHelper.arouter(RouteTable.ROUTER_ACTIVITY_SEARCH));
        mCommonTitleBar.setListener((v, action, extra) -> {
            if (action == CommonTitleBar.ACTION_LEFT_BUTTON) {
                finish();
            } else if (action == CommonTitleBar.ACTION_SEARCH) {
                IntentHelper.arouter(RouteTable.ROUTER_ACTIVITY_SEARCH);
            } else {
                IntentHelper.arouter(RouteTable.ROUTER_ACTIVITY_SEARCH);
            }
        });
        mPresenter = new ClassificationPresenter(this, new ClassificationModel());
        mPresenter.getClassificationList();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void onLoadSuccess(ClassificationBean classificationBean) {
        if (classificationBean == null) {
            return;
        }
        initMenuList(classificationBean);
    }

    /**
     * 左边一级菜单列表
     *
     * @param classificationBean
     */
    private void initMenuList(ClassificationBean classificationBean) {
        if (classificationBean.getList() != null && classificationBean.getList().size() == 0) {
            return;
        }
        mClassificationMenuListAdapter = new ClassificationMenuListAdapter(R.layout.layout_item_classification_menu_layout, classificationBean.getList());
        mMenuView.setAdapter(mClassificationMenuListAdapter);
        mClassificationMenuListAdapter.setOnItemClickListener((adapter, view, position) -> {
            mClassificationMenuListAdapter.setChecked(position);
            initChildMenuList(classificationBean, position);
        });
        initChildMenuList(classificationBean, 0);
    }

    /**
     * 右边二级菜单列表
     *
     * @param classificationBean
     * @param position
     */
    private void initChildMenuList(ClassificationBean classificationBean, int position) {
        if (!TextUtils.isEmpty(classificationBean.getList().get(position).getAdvert_pic())) {
            mImageBanner.setVisibility(View.VISIBLE);
            GlideHelper.setLoadRoundImage(this, classificationBean.getList().get(position).getAdvert_pic(), R.color.gray, mImageBanner);
            mImageBanner.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString("title", mClassificationChildMenuListAdapter.getItem(position).getName());
                IntentHelper.arouterUri(classificationBean.getList().get(position).getAdvert_url(), bundle);
            });
        } else {
            mImageBanner.setVisibility(View.GONE);
        }
        mClassificationChildMenuListAdapter = new ClassificationChildMenuListAdapter(R.layout.layout_item_classification_child_layout, classificationBean.getList().get(position).getList());
        mChildView.setLayoutManager(new GridLayoutManager(this, 3));
        mChildView.setAdapter(mClassificationChildMenuListAdapter);
        mClassificationChildMenuListAdapter.setOnItemClickListener((adapter, view, position1) -> {
            Bundle bundle = new Bundle();
            bundle.putString("title", mClassificationChildMenuListAdapter.getItem(position1).getName());
            IntentHelper.arouterUri("/pages/cat/cat?cat_id=" + mClassificationChildMenuListAdapter.getItem(position1).getId(), bundle);
        });
    }

    @Override
    public void onLoadFailed(String err) {

    }

    /**
     * 读取本地保存的搜索历史记录关键字
     *
     * @return
     */
    private List<String> getHisSearchTags() {
        String search = Hawk.get("search", null);
        if (TextUtils.isEmpty(search)) {
            return null;
        }
        List<String> keys = JSON.parseArray(search, String.class);
        return keys;
    }

}
